package com.hope.onlinestudy.fragment.message

import com.google.gson.Gson
import com.hope.onlinestudy.model.BaseModel
import com.hope.onlinestudy.model.LoginModel
import com.hope.onlinestudy.model.MessageModel
import com.hope.onlinestudy.mvp.BasePresenterImpl
import com.hope.onlinestudy.net.HttpNetUtils
import com.hope.onlinestudy.net.NetworkScheduler
import com.hope.onlinestudy.net.ProgressSubscriber
import com.hope.onlinestudy.utils.ApiUtils

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */
class MessagePresenter : BasePresenterImpl<MessageContract.View>(),
    MessageContract.Presenter {

    private val allMsg: ArrayList<MessageModel> by lazy { ArrayList<MessageModel>() }

    override fun getMsg(page: Int?, type: String?) {
        val msg = Gson().toJson(
            hashMapOf<String, Any>(
                "currentPage" to page!!,
                "msgType" to type!!,
                "userId" to ApiUtils.loginModel?.userId!!
            )
        )
        val url =
            "${ApiUtils.configModel?.sys_base_url}${ApiUtils.configModel?.sys_h5_context}${ApiUtils.toMyMsg}"
        HttpNetUtils.getInstance().getManager()
            ?.getMessage(url, ApiUtils.loginModel?.jwtToken!!, msg)
            ?.compose(NetworkScheduler.compose())
            ?.subscribe(object : ProgressSubscriber<BaseModel<List<MessageModel>>>(mView) {
                override fun onSuccess(data: BaseModel<List<MessageModel>>?, code: Int?) {
                    if (data?.list == null || data.list?.isEmpty()!!) {
                        mView?.showMsg("暂无数据")
                    } else {
                        allMsg.addAll(data.list!!)
                        mView?.msgList(allMsg)
                    }
                    mView?.reflashCommpand()
                }
            })
    }

    override fun clearData() {
        allMsg.clear()
    }
}